package com.superarai.util.net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class Proxy {

	Logger logger = Logger.getLogger(Proxy.class);

	private HttpServletRequest request;
	private HttpServletResponse response;

	// request
	private String method;
	private String uri;
	private String protocol;

	// request header
	private Map<String, String> requestHeaders = new LinkedHashMap<String, String>();

	private String host = "localhost";
	private String path = "/t2test/hello";

	private String contentType;
	private String charset;
	private int port;



	public Proxy(HttpServletRequest request, HttpServletResponse response) {
		logger.debug("Create Proxy");

		this.request = request;
		this.response = response;


		this.contentType = request.getContentType();
		this.charset = request.getCharacterEncoding();

		this.protocol = request.getProtocol();
		this.method = request.getMethod();
		this.uri = request.getRequestURI();

		this.request.getProtocol();

		Enumeration<String> headernames = request.getHeaderNames();
		StringBuilder headerValues = new StringBuilder();

		while (headernames.hasMoreElements()) {
			String name = headernames.nextElement();
			Enumeration<String> headervals = request.getHeaders(name);

			while (headervals.hasMoreElements()) {
				String val = (String) headervals.nextElement();
				headerValues.append(val);
			}
			requestHeaders.put(name, headerValues.toString());
		}
		logger.debug("end");
	}

	public void doRequest() {
		BufferedReader reader;
		Socket socket;

		try {
			socket = new Socket(host, port);
			reader = executeRequest(request, socket.getInputStream(), socket.getOutputStream());
//
//			while (true) {
//				String line = reader.readLine();
//				if (line == null) {
//					break;
//				}
//				System.out.println(line);
//			}

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private BufferedReader executeRequest(HttpServletRequest request,
			InputStream inputStream, OutputStream outputStream)
			throws IOException {
		BufferedWriter writer;
		writer = new BufferedWriter(new OutputStreamWriter(outputStream));



		StringBuilder sb = new StringBuilder();
		sb.append(method)
			.append(" ").append(uri)
			.append(" ").append("HTTP/1.0");

		writer.write(sb.toString());
		writer.newLine();
		sb.delete(0, sb.length());




		writer.newLine();
		writer.flush();

		return null;
	}


	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setHost(String hostname) {
		this.host = hostname;
	}

	public String getHost() {
		return host;
	}

}
